﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Co9320.NamaAlert.Model.TagServer;

namespace Co9320.NamaAlert.CommunicationControl
{
	internal class InteractiveTransactor
	{
		private readonly object SyncRoot;

		Dictionary<LiveInfo, object> Dict;

		public InteractiveTransactor()
		{
			SyncRoot = new object();
			Dict = new Dictionary<LiveInfo, object>();
		}

		public void AddEntry(string StationId, string Caster)
		{
			if (StationId == null || Caster == null || 
				StationId.Trim().Length == 0 || Caster.Trim().Length == 0)
			{
				return;
			}

			LiveInfo info = new LiveInfo(StationId, Caster);

			lock (SyncRoot)
			{
				if (Dict.ContainsKey(info))
				{
					return;
				}
				else
				{
					Dict.Add(info, null);
				}
			}
		}

		public GetParameter[] GetEntry()
		{
			lock (SyncRoot)
			{
				if (Dict.Count == 0)
				{
					return null;
				}

				var Ret= Dict.Select(tmp =>
				{
					GetParameter r = new GetParameter();
					r.StationId = tmp.Key.StationId;
					r.Caster = tmp.Key.Caster;
					return r;
				}).ToArray();

				Dict.Clear();
				return Ret;
			}
		}
	}
}
